package com.example.index.system.fitsystemwindow

import android.annotation.SuppressLint
import android.os.Bundle
import android.preference.PreferenceManager
import android.util.Log
import android.view.View
import com.cat.annotation.Index
import com.example.index.BaseActivity
import com.example.index.R
import com.example.index.extension.quickStartActivity
import com.example.index.extension.setImmersive
import com.example.index.system.CoordinatorLayoutFitSystemWindowActivity
import kotlinx.android.synthetic.main.activity_fit_system_window.*


@Index(
    keywords = "system/fitsSystemWindows/statusBar/状态栏",
    link = "https://juejin.im/entry/59469d3f61ff4b006cf363ca"
)
@SuppressLint("ApplySharedPref")
class FitSystemWindowActivity : BaseActivity() {

    val TAG = "FitSystemWindowActivity"

    val KEY_UI_FLAG = "KeyUiFlagSet"
    val KEY_FITSYSTEMWINDOW = "KeyFitSystemWindow"

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fit_system_window)


        val sharePref = PreferenceManager.getDefaultSharedPreferences(this)

        val uiFlagSet = sharePref.getBoolean(KEY_UI_FLAG, false)
        val fitSystemWindow = sharePref.getBoolean(KEY_FITSYSTEMWINDOW, false)

        headerText.text = "${if (uiFlagSet) "  immersive ui flag is true\n" else "  immersive ui flag is false\n"}  ${
        if (fitSystemWindow) "fitSystemWindows is true\n" else "fitSystemWindows is false\n"}"

        Log.i(TAG, "onCreate:$uiFlagSet   $fitSystemWindow ")

        fl_root.fitsSystemWindows = fitSystemWindow

        if (uiFlagSet) window.setImmersive()

    }

    fun setFitSystemWindow(view: View) {
        booleanPrefSet(KEY_FITSYSTEMWINDOW, true)
    }

    fun resetFitSystemWindow(view: View) {
        booleanPrefSet(KEY_FITSYSTEMWINDOW, false)
    }

    fun uiFlagSet(view: View) {
        booleanPrefSet(KEY_UI_FLAG, true)

    }

    fun uiFlagUnset(view: View) {
        booleanPrefSet(KEY_UI_FLAG, false)
    }


    private fun booleanPrefSet(key: String, value: Boolean) {
        val sharePref = PreferenceManager.getDefaultSharedPreferences(this)
        sharePref.edit()
            .putBoolean(key, value)
            .commit()
        recreate()
    }

    fun goCoordinatorLayoutPage(view: View) {
        quickStartActivity(CoordinatorLayoutFitSystemWindowActivity::class.java)
    }
}
